Vue.component('config-log', {//修改记录组件
    template: `<el-dialog title="修改记录" :visible.sync="visibleDialog" width="80vw">
    <el-table :data="tableData" stripe border height="50vh" style="width: 100%">
        <el-table-column align="center" type="index" label="序号" width="50"></el-table-column>
        <el-table-column prop="Gift_Points" label="下单后每1元赠送多少积分给客户"></el-table-column>
        <el-table-column prop="Points_Deduction" label="满足多少元可以抵扣积分"></el-table-column>
        <el-table-column prop="Deduction_Ratio" label="积分抵扣金额比例"></el-table-column>
        <el-table-column prop="Deduction_Limit" label="每笔订单最多抵扣多少积分"></el-table-column>
        <el-table-column prop="Row_Status" label="每笔订单最多抵扣多少积分"></el-table-column>
        <el-table-column prop="Operation_Datetime" label="操作时间"></el-table-column>
    </el-table>
    <el-pagination style="margin-top: 5px;" background @size-change="handleSizeChange"
        @current-change="handleCurrentChange" :current-page="form.page" :page-sizes="[10, 20, 30, 50]"
        :page-size="form.limit" layout="total, sizes, prev, pager, next, jumper" :total="total">
    </el-pagination>
    <span slot="footer" class="dialog-footer">
        <el-button @click="$emit('update:visible',false)">取消</el-button>
        <el-button type="primary" @click="submitForm('contract')">提交</el-button>
    </span></el-dialog>`,
    props: ['refresh', 'visible'],
    data() {
        return {
            visibleDialog: false,
            refreshDialog: false,
            tableData: [],
            form: {
                SQLSerialNumber: "P_business_points_config",
                PaginationNumber: 1,
                RowAmount: 10,
                ExtensionCondition: '',
                Condition: ''
            },
            total: 0,
        }
    },
    watch: {
        visible() {
            this.visibleDialog = this.visible
        },
        refresh(val) {
            this.refreshDialog = this.refresh
        },
        visibleDialog(val) {
            this.$emit('update:visible', val)
        },
        refreshDialog(val) {
            if (val) {
                this.getList(this.form);
            }
            this.$emit('update:refresh', val);
        }
    },
    mounted() {
        this.getList(this.form);
    },
    methods: {
        handleSizeChange(val) {
            this.form.PaginationNumber = 1;
            this.form.RowAmount = val;
            this.getList(this.form)
        },
        handleCurrentChange(val) {
            this.form.PaginationNumber = val;
            this.getList(this.form)
        },
        getList(data) {
            const that = this
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalPaginationQueryBySQLSerialNumber`,
                type: 'POST',
                data,
                success: function (Result) {
                    that.tableData = Result.Rows;
                    that.total = Result.TotalRows;
                },
                complete: function (XMLHttpRequest) {
                    that.refreshDialog = false;
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        }
    }
})
new Vue({
    el: '#app',
    data() {
        return {
            form: {
                Total: 1,
                Variance: 1,
                Product_Quality:0,
                Transaction_Situation:0,
                Customer_Satisfaction:0,
            },
            page: {
                SQLSerialNumber: "P_business_points_detail_config",
                PaginationNumber: 1,//页数
                RowAmount: 10,//每页数据条数
                Article_Name: '',
                Supplier_SerialNumber: '',
                Brand_SerialNumber: '',
                ExtensionCondition: '',
                Condition: ''
            },
            total: 0,
            tableData: [],
            supplier: [],
            brand: [],
            loading: false,
            OldForm: null,
            rules: {},
            activeNames: ['1'],
            dialogVisible: false,
            Refresh: false,
        }
    },
    mounted() {
        this.getData();
    },
    methods: {
        onSerach() {			//查询方法
            this.form.PaginationNumber = 1;
            this.getList(this.page)
        },
        handleSizeChange(val) {
            this.page.PaginationNumber = 1;
            this.page.RowAmount = val;
            this.getList(this.page)
        },
        handleCurrentChange(val) {
            this.page.PaginationNumber = val;
            this.getList(this.page)
        },
        getData() {
            const that = this;
            const Condition = "Row_Status='0'"
            $.ajax({
                headers: requestHeaders(),
                url: `${BASE_URL2}/generalBusiness/generalQueryByTableName`,
                type: 'POST',
                data: {
                    Data: 'group_credit_score_config',
                    Condition:"Row_Status='0' ORDER BY Row_Status DESC LIMIT 1",
                },
                success: function (data) {
                    if (data.length == 1) {
                        console.log(data[0]);
                        let form = deepClone(data[0]);
                        form.Total = Number(form.Total);
                        form.Variance = Number(form.Variance);
                        form.Customer_Satisfaction = Number(form.Customer_Satisfaction);
                        form.Product_Quality = Number(form.Product_Quality);
                        form.Transaction_Situation = Number(form.Transaction_Situation);
                        that.form = form;
                        let OldForm = deepClone(data[0]);
                        OldForm.Total = Number(OldForm.Total);
                        OldForm.Variance = Number(OldForm.Variance);
                        OldForm.Customer_Satisfaction = Number(OldForm.Customer_Satisfaction);
                        OldForm.Product_Quality = Number(OldForm.Product_Quality);
                        OldForm.Transaction_Situation = Number(OldForm.Transaction_Situation);
                        that.OldForm = OldForm;
                    }else{
                        that.OldForm = null
                    }
                },
                complete: function (XMLHttpRequest) {
                    requestPretreatment(XMLHttpRequest);
                },
                dataType: "json"
            });
        },
        checkModify() {
            if (this.OldForm) {
                return this.form.Total == this.OldForm.Total &&
                    this.form.Variance == this.OldForm.Variance &&
                    this.form.Product_Quality == this.OldForm.Product_Quality &&
                    this.form.Transaction_Situation == this.OldForm.Transaction_Situation &&
                    this.form.Customer_Satisfaction == this.OldForm.Customer_Satisfaction
            } else {
                return this.form.Row_Status == '1'
            }
        },
        submitForm(formName) {
            const that = this
            this.$refs[formName].validate((valid) => {
                if (valid) {
                    if (that.checkModify()) {
                        that.$message({
                            message: '当前设置并未改变！',
                            type: 'warning'
                        })
                        return
                    }
                    that.loading = true;
                    $.ajax({
                        headers: requestHeaders(),
                        url: `${BASE_URL2}/generalBusiness/generalSave`,
                        type: 'POST',
                        data: {
                            Data: JSON.stringify({ Add: { group_credit_score_config: [that.form] } })
                        },
                        success: function (res) {
                            if (res.Status === 'success') {
                                that.$message.success("配置成功")
                                // that.getData();
                                that.Refresh = true;
                            } else {
                                that.$message.error("提交失败，请稍后再试")
                            }
                        },
                        error: function () {
                            that.$message.error("操作失败，请稍后再试")
                        },
                        complete: function (XMLHttpRequest) {
                            that.loading = false
                            requestPretreatment(XMLHttpRequest);
                        }
                    })
                } else {
                    if (!that.activeNames.some(i => i == '1')) {
                        that.activeNames.push('1')
                    }
                    return false;
                }
            });
        },
    }
})